package com.daisuxia.back.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.daisuxia.back.dao.IFengKongDao;
import com.daisuxia.back.dao.IMmanLoanCollectionOrderDao;
import com.daisuxia.back.dao.IPaginationDao;
import com.daisuxia.back.result.JsonResult;
import com.daisuxia.back.utils.IdGen;
import com.daisuxia.constant.Constant;
import com.daisuxia.web.pojo.CollectionAdvice;
import com.daisuxia.web.pojo.FengKong;
import com.daisuxia.web.pojo.MmanLoanCollectionOrder;
import com.daisuxia.web.util.PageConfig;
@Service
public class FengKongService implements IFengKongService {
	private static Logger logger = LoggerFactory.getLogger(FengKongService.class);
	
	@Autowired
	private IPaginationDao<FengKong> paginationDao;
	@Autowired
	private IFengKongDao fengKongDao;
	@Autowired
	private IMmanLoanCollectionOrderDao mmanLoanCollectionOrderDao;
	@Autowired
	private IAuditCenterService auditCenterService;
	
	@Override
	public PageConfig<FengKong> findPage(HashMap<String, Object> params) {
		params.put(Constant.NAME_SPACE, "FengKong");
		return paginationDao.findPage("findAll", "findAllCount", params,null);
	}

	@Override
	public List<FengKong> getFengKongList() {
		return fengKongDao.getFengKongList();
	}

	@Override
	public FengKong getFengKongById(Integer id) {
		return fengKongDao.getFengKongById(id);
	}

	@Override
	public JsonResult updateFengKong(FengKong fengKong) {
		JsonResult result=new JsonResult("-1","审核修改风控标签失败");
		try{
			if(fengKongDao.update(fengKong)>0){
				result.setCode("0");
				result.setMsg("审核修改风控标签成功");
			}
		}catch(Exception e){
			logger.error("FengKongService updateFengKong", e);
		}
		return result;
	}

	@Override
	public JsonResult insert(FengKong fengKong) {
		JsonResult result=new JsonResult("-1","添加风控标签失败");
		try{
			if(fengKongDao.insert(fengKong)>0){
				result.setCode("0");
				result.setMsg("添加风控标签成功");
			}
		}catch(Exception e){
			logger.error("FengKongService insert", e);
		}
		return result;
	}

	@Override
	public JsonResult saveCollectionAdvice(Map<String, String> params) {
		JsonResult result=new JsonResult("-1","添加失败");
		try{
			String orderId = params.get("id");
			MmanLoanCollectionOrder mmanLoanCollectionOrder = mmanLoanCollectionOrderDao.getOrderById(orderId);

			CollectionAdvice collectionAdvice = new CollectionAdvice();

			collectionAdvice.setId(IdGen.uuid());
			collectionAdvice.setOrderId(params.get("id"));
			collectionAdvice.setLoanId(mmanLoanCollectionOrder.getLoanId());
			collectionAdvice.setPayId(mmanLoanCollectionOrder.getPayId());
			collectionAdvice.setUserId(mmanLoanCollectionOrder.getUserId());
			collectionAdvice.setLoanUserName(mmanLoanCollectionOrder.getLoanUserName());
			collectionAdvice.setLoanUserPhone(mmanLoanCollectionOrder.getLoanUserPhone());
			collectionAdvice.setBackUserId(Integer.valueOf(params.get("backUserId")));
			collectionAdvice.setUserName(params.get("userName"));
			collectionAdvice.setFengkongIds(params.get("fengkongIds"));
			collectionAdvice.setFkLabels(params.get("fkLabels"));
			collectionAdvice.setCreateDate(new Date());
			collectionAdvice.setStatus(params.get("status"));
			if(fengKongDao.insertCollectionAdvice(collectionAdvice)>0){
				result.setCode("0");
				result.setMsg("添加成功");
				//如果有催收建议则添加(只有在该订单以前没有建议或者以前建议拒绝时候才能申请)
				if(!"3".equals(params.get("status"))&&(StringUtils.isEmpty(mmanLoanCollectionOrder.getCsstatus())||Constant.CS_STATUS_REFUSE==mmanLoanCollectionOrder.getCsstatus())) {
					result = auditCenterService.saveSuggestionApply(params);
				}
			}
			//若是有催收建议则申请催收建议
		}catch(Exception e){
			logger.error("FengKongService insert", e);
		}
		return result;
	}

}
